home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Applications / Assorted Programs / PCB Cad / Event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-06  |  4.8 KB  |  206 lines  |  [TEXT/KAHL]

  1. /********************************************************************
  2.  *                                                                     *
  3.  * the Mac version of the PCB Cad program written by Randy Nevin    *
  4.  * was ported by Carlos Bazzarella, University of Waterloo.            *
  5.  *                                                                    *
  6.  ********************************************************************/
  7.  
  8.  
  9. #define BASE_RES_ID            400
  10. #define NIL_POINTER            0L
  11. #define MOVE_TO_FRONT        -1L
  12. #define REMOVE_ALL_EVENTS    0
  13.  
  14. #define LEAVE_WHERE_IT_IS    FALSE
  15. #define NORMAL_UPDATES        TRUE
  16.  
  17. #define SLEEP                0L
  18. #define NIL_MOUSE_REGION    0L
  19. #define WNE_TRAP_NUM        0x60
  20. #define UNIMPL_TRAP_NUM        0x9F
  21. #define SUSPEND_RESUME_BIT    0x0001
  22. #define ACTIVATING            1
  23. #define RESUMING            1
  24.  
  25. #define DRAG_THRESHOLD        30
  26. #define MIN_WINDOW_HEIGHT    50
  27. #define MIN_WINDOW_WIDTH    50
  28. #define SCROLL_BAR_PIXELS    16
  29.  
  30.  
  31. WindowPtr        gPictWindow;
  32. Boolean            gDone, gWNEImplemented;
  33. EventRecord        gTheEvent;
  34. Rect            gDragRect, gSizeRect;
  35.  
  36. char HandleEvent();
  37.  
  38.  
  39. SetUpMacInt()
  40. {
  41.    ToolBoxInit();
  42.    WindowInit();
  43.    SetUpDragRect();
  44.    SetUpSizeRect();   
  45. }
  46.  
  47.  
  48.  
  49. ToolBoxInit()
  50. {
  51.    InitGraf( &thePort );
  52.    InitFonts();
  53.    FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  54.    InitWindows();
  55.    InitMenus();
  56.    TEInit();
  57.    InitDialogs( NIL_POINTER );
  58.    InitCursor();
  59. }
  60.  
  61.  
  62. WindowInit()
  63. {
  64.    gPictWindow = GetNewWindow ( BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT);
  65.    /*ShowWindow (gPictWindow);*/
  66.    
  67.    /*SelectWindow (gPictWindow);*/
  68. }
  69.  
  70.  
  71.  
  72.  
  73. SetUpDragRect()
  74. {
  75.    gDragRect = screenBits.bounds;
  76.    gDragRect.left += DRAG_THRESHOLD;
  77.    gDragRect.right -= DRAG_THRESHOLD;
  78.    gDragRect.bottom -= DRAG_THRESHOLD;
  79. }
  80.  
  81.  
  82. SetUpSizeRect()
  83. {
  84.    gSizeRect.top = MIN_WINDOW_HEIGHT;
  85.    gSizeRect.left = MIN_WINDOW_WIDTH;
  86.    
  87.    gSizeRect.bottom = screenBits.bounds.bottom - screenBits.bounds.top;                                             
  88.    gSizeRect.right = screenBits.bounds.right - screenBits.bounds.left;                                             
  89. }
  90.  
  91.  
  92. char getKey()
  93. {
  94.    char c;
  95.    
  96.    gDone = FALSE;
  97.    gWNEImplemented = ( NGetTrapAddress( WNE_TRAP_NUM, ToolTrap ) !=
  98.               NGetTrapAddress( UNIMPL_TRAP_NUM, ToolTrap ));
  99.    while ( gDone == FALSE )
  100.    {
  101.       c = HandleEvent();
  102.    }
  103.    return c;
  104. }
  105.  
  106.  
  107. char HandleEvent()
  108. {   
  109.    if ( gWNEImplemented )
  110.       WaitNextEvent ( everyEvent , &gTheEvent , SLEEP,
  111.             NIL_MOUSE_REGION);
  112.    else                 
  113.    {
  114.       SystemTask();
  115.       GetNextEvent( everyEvent, &gTheEvent );
  116.    }
  117.    
  118.    switch ( gTheEvent.what )
  119.    {
  120.         case mouseDown :
  121.                HandleMouseDown();
  122.                break;
  123.         case keyDown   :
  124.         case autoKey   :
  125.                gDone = TRUE;
  126.                return ( gTheEvent.message & charCodeMask );
  127.                break;
  128.         default :
  129.                return (0);
  130.                break;
  131.    }
  132. }
  133.  
  134. HandleMouseDown()
  135. {
  136.    WindowPtr    whichWindow;
  137.    short int    thePart;
  138.    long            windSize;
  139.    GrafPtr        oldPort;
  140.    
  141.    thePart = FindWindow ( gTheEvent.where, &whichWindow);
  142.    switch ( thePart )
  143.    {
  144.       case inSysWindow :
  145.               SystemClick ( &gTheEvent, whichWindow );
  146.               break;
  147.       case inDrag      :
  148.               DragWindow (whichWindow, gTheEvent.where, &gDragRect );
  149.               break;
  150.       case inContent   :
  151.               SelectWindow (whichWindow);
  152.               break;
  153.       case inGrow      :
  154.               windSize = GrowWindow ( whichWindow, gTheEvent.where,
  155.                               &gSizeRect );
  156.               if ( windSize != 0  )
  157.               {
  158.                  GetPort ( &oldPort);
  159.                  SetPort ( whichWindow );
  160.                  EraseRect ( &whichWindow->portRect );
  161.                  SizeWindow (whichWindow, LoWord( windSize),
  162.                       HiWord( windSize), NORMAL_UPDATES);
  163.                  InvalRect ( &whichWindow->portRect );
  164.                  SetPort ( oldPort );
  165.               }
  166.               break;
  167.        case inGoAway    :
  168.               gDone = TRUE;
  169.               break;
  170.        case inZoomIn    :
  171.        case inZoomOut   :
  172.               if ( TrackBox (whichWindow, gTheEvent.where, thePart ))
  173.               {
  174.                  GetPort (&oldPort );
  175.                  SetPort ( whichWindow );
  176.                  EraseRect (&whichWindow->portRect );
  177.                  ZoomWindow (whichWindow, thePart, LEAVE_WHERE_IT_IS );
  178.                  InvalRect (&whichWindow->portRect );
  179.                  SetPort (oldPort );
  180.               }
  181.               break;
  182.    }
  183. }
  184.  
  185.  
  186. DrawGrid ( RectPtr )
  187. Rect    *RectPtr;
  188. {
  189.    int         x,y;
  190.    
  191.    PenPat ( ltGray );
  192.    
  193.    for ( y = RectPtr->top; y <= RectPtr->bottom; y = y + 25 )
  194.    {
  195.       MoveTo ( RectPtr->left, y);
  196.       LineTo ( RectPtr->right, y);
  197.    }
  198.    
  199.    for ( x = RectPtr->left; x <= RectPtr->right; x = x + 23 )
  200.    {
  201.       MoveTo ( x, RectPtr->top );
  202.       LineTo ( x, RectPtr->bottom );
  203.    }
  204.    
  205.    PenPat ( black );
  206. }